package cn.com.sd.common.util;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSON;

public class MyStringUtils {

	public static Integer translateLongToInteger(Long value) {
		if (value == null || value <= 0) {
			return -1;
		}
		try {
			return Integer.parseInt(value.toString());
		} catch (Exception e) {
			return -1;
		}
	}
	
	public static Integer translateStringToInteger(String value) {
		try {
			return Integer.parseInt(value);
		} catch (Exception e) {
			return -1;
		}
	}
	
	/**
	 * 判断字符串是否为空
	 * @param str
	 * @return
	 * @auther yaoming
	 */
	public static boolean isEmpty(String str){
		if(null==str||str.trim().equals(""))
			return true;
		return false;
	}
	
	public static Long getCurrentTime() {
		return System.currentTimeMillis();
	}

	/** 将时间戳转换成前端界面显示日期格式 (精确到天) */
	public static String dateFormat(long date) {

		Date dt = new Date(date / 1000);
		DateFormat df = new SimpleDateFormat("yyyy.MM.dd");
		String time = df.format(dt);

		return time;
	}

	/** 将时间戳格式转换成前端界面显示的时间（精确到分） */
	public static String dateFormat(String date) {
		if (StringUtils.isEmpty(date) || date.length() < 13) {
			return "";
		}
		Date dt = new Date(Long.parseLong(date.substring(0, 13)));

		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");

		return df.format(dt);
	}

	/** 获取传入时间与当前时间的时间差 */
	public static String differTime(String actionTime) {

		if (StringUtils.isEmpty(actionTime) || actionTime.length() < 13) {
			return "";
		}
		int YEAR = Calendar.getInstance().get(Calendar.YEAR);
		int MONTH = Calendar.getInstance().get(Calendar.MONTH) + 1;
		int DAY = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
		int HOUR = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
		int MIN = Calendar.getInstance().get(Calendar.MINUTE);

		int oldTotalTime = getTotalTime(actionTime);
		int nowTotalTime = MIN + HOUR * 60 + DAY * 60 * 24 + MONTH * 60 * 24
				* 31 + YEAR * 60 * 24 * 31 * 12;

		return getDifferTime(nowTotalTime, oldTotalTime, actionTime);
	}

	/** 时间比对的方法 */
	public static String getDifferTime(int nowTotalTime, int oldTotalTime,
			String actionTime) {
		if (nowTotalTime <= oldTotalTime) {
			return "小于1分钟前";
		} else if (nowTotalTime > oldTotalTime
				&& nowTotalTime - oldTotalTime < 60) {
			return nowTotalTime - oldTotalTime + "分钟前";
		} else if (nowTotalTime > oldTotalTime
				&& nowTotalTime - oldTotalTime < 60 * 24) {
			int h = (nowTotalTime - oldTotalTime) / 60;
			return h + "小时" + (nowTotalTime - oldTotalTime - h * 60) + "分钟前";
		} else if (nowTotalTime > oldTotalTime
				&& nowTotalTime - oldTotalTime < 60 * 24 * 31) {
			int d = (nowTotalTime - oldTotalTime) / 1440;
			return d + "天"
					+ ((nowTotalTime - oldTotalTime - d * 1440) / 60 + 1)
					+ "小时前";
		} else {
			Date dt = new Date(Long.parseLong(actionTime.substring(0, 13)));
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			int nowYear = Calendar.getInstance().get(Calendar.YEAR);
			int oldYear = Integer.parseInt(df.format(dt).substring(0,
					df.format(dt).indexOf("-")));
			if (nowYear != oldYear && nowYear > oldYear) {
				df = new SimpleDateFormat("yyyy-MM-dd");
			}
			return df.format(dt);
		}
	}

	/** 封装计算时间总和的方法 */
	public static int getTotalTime(String actionTime) {

		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Date dt = new Date(Long.parseLong(actionTime.substring(0, 13)));
		String time = df.format(dt);
		// 获取传入时间的年，月，日，小时，分钟
		int year = Integer.parseInt(time.substring(0, time.indexOf("-")));
		int month = Integer.parseInt(time.substring(time.indexOf("-") + 1,
				time.lastIndexOf("-")));
		int day = Integer.parseInt(time.substring(time.lastIndexOf("-") + 1,
				time.indexOf(" ")));
		int hour = Integer.parseInt(time.substring(time.indexOf(" ") + 1,
				time.indexOf(":")));
		int min = Integer.parseInt(time.substring(time.indexOf(":") + 1,
				time.length()));

		// System.out.println(year+":"+month+":"+day+":"+hour+":"+min);

		return min + hour * 60 + day * 60 * 24 + month * 60 * 24 * 31 + year
				* 60 * 24 * 31 * 12;
	}
	
	/** 为ajax请求封装json数据 */
	public static void outputJSONResult(String errorCode, String msg,
			String jsonData, HttpServletResponse response) {

		String result = construtResult(errorCode, msg, jsonData);
		outputJSONResult(result, response);

	}

	public static String construtResult(String errorCode, String msg, String jsonData) {
		JSONModel jsonModel = new JSONModel(errorCode, msg, jsonData);

		String result = JSON.toJSONString(jsonModel);
		result = "{\"result\":[" + result + "]}";
		result = result.replace("\\\"", "\"");
		result = result.replaceAll("\"\\{", "{");
		result = result.replaceAll("\\}\"", "}");

		return result;

	}
	
	/** 为ajax请求封装json数据 */
	public static void outputJSONResult(String result,
			HttpServletResponse response) {

		try {
			response.setHeader("ContentType", "text/json;charset=UTF-8");
			response.setCharacterEncoding("utf-8");
			PrintWriter pw = response.getWriter();

			pw.write(result);
			pw.flush();
			pw.close();

		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
